#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"
#include <linux/input.h>

static struct input_event inputevent; 

int main(int argc, char *argv[])
{
	int fd, ret;
	char *filename;
	
	if(argc != 2){
		printf("Error Usage!\r\n");
		return -1;
	}

	filename = argv[1];

	/* 打开key驱动 */
	fd = open(filename, O_RDWR);
	if (fd < 0){
		printf("file %s open failed!\r\n", argv[1]);
		return -1;
	}

	/* 循环读取按键值数据！ */
	while(1) {
		ret = read(fd, &inputevent, sizeof(struct input_event));
		if (ret > 0) {
			switch(inputevent.type) {
				case EV_KEY:
					if (inputevent.code < BTN_MISC) {
						printf("key %d %s\r\n", inputevent.code, inputevent.value ? "press" : "release");
					}
					else {
						printf("button %d %s\r\n", inputevent.code, inputevent.value ? "press" : "release");
					}
					break;
				case EV_SYN:
					break;
				case EV_REL:
					break;
				case EV_ABS:
					break;
			}
		}
		else {
			printf("读取数据失败\r\n");
		}
	}

	close(fd); /* 关闭文件 */
	return 0;
}
